//
//  TabBarController.m
//  Gekko
//
//  Created by jose on 02/06/13.
//  Copyright (c) 2013 Jose Luis Lopez. All rights reserved.
//

#import "TabBarController.h"

@interface TabBarController ()

@end

@implementation TabBarController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.tabBar setBackgroundImage:[UIImage imageNamed:@"tab_bar"]];
    [self.tabBar setSelectionIndicatorImage:[UIImage imageNamed:@"tab_selected"]];
    [self.tabBar setSelectedImageTintColor:[UIColor whiteColor]];
    [self.tabBar setTintColor:[UIColor whiteColor]];
    
    for (UITabBarItem *item in [self.tabBar items]) {
        item.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0);
    }
    [[[self.tabBar items] objectAtIndex:0] setFinishedSelectedImage:[UIImage imageNamed:@"tab_history_icon"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab_history_icon"]];
    [[[self.tabBar items] objectAtIndex:1] setFinishedSelectedImage:[UIImage imageNamed:@"tab_plus_icon"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab_plus_icon"]];
    [[[self.tabBar items] objectAtIndex:2] setFinishedSelectedImage:[UIImage imageNamed:@"tab_options_icon"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab_options_icon"]];
    
    [self setSelectedIndex:1];
    
    for(UIView *view in self.view.subviews)
    {
        if(![view isKindOfClass:[UITabBar class]])
        {
            [view setFrame:CGRectMake(0, 0, 320, [[UIScreen mainScreen] bounds].size.height)];
        }
    
    }
    
    [ReceiptsHolder sharedReceiptsHolder].arrayReceipts = [ArchiverUnarchiver unarchiveReceipts];
}

- (void)viewDidAppear:(BOOL)animated {
    
    if (![[NSUserDefaults standardUserDefaults] valueForKey:kKeyUserId]) {
        
        [self performSegueWithIdentifier:@"SegueToLogin" sender:self];
    }
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
 

    if (self.selectedIndex == 1 && [item isEqual:[[tabBar items] objectAtIndex:self.selectedIndex]]) {
        
        // Here reset the singleton temp receipt
        [Receipt resetSharedReceipt];
        
    } else if (self.selectedIndex == 0 && ![item isEqual:[[tabBar items] objectAtIndex:self.selectedIndex]]) {
        
        [[[self childViewControllers] objectAtIndex:0] popToRootViewControllerAnimated:NO];
        [Receipt resetSharedReceipt];
    }
}



@end
